/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import exatrez.dao.PenggunaDAO;
import exatrez.dao.VesselDAO;
import exatrez.manager.ObservationManager;
import exatrez.manager.VesselManager;
import exatrez.model.InfoAudit;
import exatrez.model.Observation;
import exatrez.model.User;
import exatrez.model.Vessel;
import exatrez.stripes.BaseActionBean;
import exatrez.stripes.config.AuthRequired;
import java.util.Date;
import java.util.List;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author wan.fairul
 */
@UrlBinding("/adminVessel")
@AuthRequired
public class VesselAction extends BaseActionBean {
    
    private static Logger LOG = LoggerFactory.getLogger(VesselAction.class);
    @Inject
    private VesselDAO vesselDAO;
    @Inject
    private VesselManager vesselManager;
    @Inject
    private PenggunaDAO penggunaDAO;
    private Vessel vessel;
    private String superintendence;
    private String crewExe;
    private String procExe;
    private List<Vessel> listofVessel;
    @Inject
    ObservationManager observationManager;
    
    @DefaultHandler
    public Resolution add_vessel() {
        vessel = new Vessel();
        listofVessel = vesselManager.findAll();
        return new JSP("admin/vessel.jsp");
    }
    
    public Resolution saveVessel() {
        LOG.debug("masuk");
        Vessel v = vesselManager.findVesselName(vessel.getVesselName());
        User user = getContext().getCurrentUser();
        if (v == null) {
            InfoAudit ia = new InfoAudit();
            ia.setCreateDate(new Date());
            ia.setCreateId(user);
            vessel.setInfoAudit(ia);
            vessel.setActive('Y');
            superintendence = getContext().getRequest().getParameter("superintendence");
            crewExe = getContext().getRequest().getParameter("crewExe");
            procExe = getContext().getRequest().getParameter("procExe");
            LOG.info(superintendence);
            LOG.info(crewExe);
            LOG.info(procExe);
            User p = penggunaDAO.findById(superintendence);
            User p2 = penggunaDAO.findById(crewExe);
            User p3 = penggunaDAO.findById(procExe);
            vessel.setSuperintendence(p);
            vessel.setCrewExe(p2);
            vessel.setProcExe(p3);
            vesselManager.saveOrUpdate(vessel);
            addSimpleMessage("Success");
            LOG.debug("finish");
        } else {
            addSimpleError("Duplicate Vessel!");
        }
        return add_vessel();
    }
    
    public Resolution deleteVessel() {
        String vesselId = getContext().getRequest().getParameter("vesselId");
        if (vesselId != null) {
            vessel = vesselDAO.findById(Integer.parseInt(vesselId));
            if (vessel != null) {
                vesselManager.deleteVessel(vessel);
                addSimpleMessage("Success Delete");
            }
        }
        return new RedirectResolution(VesselAction.class, "add_vessel");
    }
    
    public Resolution editVessel() {
        String vesselId = getContext().getRequest().getParameter("vesselId");
        if (vesselId != null) {
            vessel = vesselDAO.findById(Integer.parseInt(vesselId));
        }
        return new JSP("admin/edit_vessel.jsp").addParameter("popup", true);
    }
    
    public Resolution saveEditVessel() {
        String vesselId = getContext().getRequest().getParameter("vessel.vesselId");
        String userid = getContext().getRequest().getParameter("vessel.superintendence.userId");
        String userid2 = getContext().getRequest().getParameter("vessel.crewExe.userId");
        String userid3 = getContext().getRequest().getParameter("vessel.procExe.userId");
        User p = penggunaDAO.findById(userid);
        User p2 = penggunaDAO.findById(userid2);
        User p3 = penggunaDAO.findById(userid3);
        User user = getContext().getCurrentUser();
        if (vesselId != null) {
            Vessel vesselabbr = vesselManager.findVesselAbbr(vesselId, vessel.getVesselAbbr());
            if (vesselabbr != null) {
                addSimpleError("Duplicate Vessel Abbreviation!");
                return add_vessel();
            }
            Vessel v = vesselDAO.findById(Integer.parseInt(vesselId));
            InfoAudit ia = v.getInfoAudit();
            v.setVesselAbbr(vessel.getVesselAbbr());
            v.setActive(vessel.getActive());
            v.setImo(vessel.getImo());
            v.setGross(vessel.getGross());
            v.setSuperintendence(p);
            v.setCrewExe(p2);
            v.setProcExe(p3);
            ia.setUpdateDate(new Date());
            ia.setUpdateId(user);
            v.setInfoAudit(ia);
            vesselManager.saveOrUpdate(v);
            List<Observation> listobservation = observationManager.findAllVesselId(String.valueOf(v.getVesselId()));
            if (listobservation != null) {
                for (Observation ob : listobservation) {
                    ob.setAssignee(p);
                    observationManager.saveOrUpdate(ob);
                }
            }
            addSimpleMessage("Success Update");
            
        }
        
        return new RedirectResolution(VesselAction.class, "add_vessel");
    }
    
    public VesselManager getVesselManager() {
        return vesselManager;
    }
    
    public void setVesselManager(VesselManager vesselManager) {
        this.vesselManager = vesselManager;
    }
    
    public VesselDAO getVesselDAO() {
        return vesselDAO;
    }
    
    public void setVesselDAO(VesselDAO vesselDAO) {
        this.vesselDAO = vesselDAO;
    }
    
    public Vessel getVessel() {
        return vessel;
    }
    
    public void setVessel(Vessel vessel) {
        this.vessel = vessel;
    }
    
    public PenggunaDAO getPenggunaDAO() {
        return penggunaDAO;
    }
    
    public void setPenggunaDAO(PenggunaDAO penggunaDAO) {
        this.penggunaDAO = penggunaDAO;
    }
    
    public String getSuperintendence() {
        return superintendence;
    }
    
    public void setSuperintendence(String superintendence) {
        this.superintendence = superintendence;
    }
    
    public List<Vessel> getListofVessel() {
        return listofVessel;
    }
    
    public void setListofVessel(List<Vessel> listofVessel) {
        this.listofVessel = listofVessel;
    }
    
    public ObservationManager getObservationManager() {
        return observationManager;
    }
    
    public void setObservationManager(ObservationManager observationManager) {
        this.observationManager = observationManager;
    }
    
    public String getCrewExe() {
        return crewExe;
    }
    
    public void setCrewExe(String crewExe) {
        this.crewExe = crewExe;
    }
    
    public String getProcExe() {
        return procExe;
    }
    
    public void setProcExe(String procExe) {
        this.procExe = procExe;
    }
}
